using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LevelPrototype
{
    public class LevelPrototype : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        public static SpriteBatch spriteBatch;
        public static SpriteFont font;
        
        private Map map;        
        private List<Tile> allTiles;
        private TileFactory tileFactory;

        public LevelPrototype()
        {
            allTiles = new List<Tile>();
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
            this.graphics.PreferredBackBufferWidth = map.TileWidth * map.Width;
            this.graphics.PreferredBackBufferHeight = map.TileHeight * map.Height;
            this.graphics.ApplyChanges();            
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            this.map = Map.Load(Path.Combine(Content.RootDirectory, "secondlevel.tmx"), Content);
            font = Content.Load<SpriteFont>("SomeFont");

            // get the tileset that is used in the logical layer
            var tileSet = (from t in this.map.Tilesets where t.Name == "logicalTiles" select t).First();

            // get the logical layer (the superimposed path/mountpoints/etc)
            var logicalLayer = (from l in this.map.Layers where l.Name == "logical" select l).First();

            this.tileFactory = new TileFactory();

            // create objects
            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    int tileIndex = logicalLayer.GetTile(x, y);
                    TilePropertyList propertyList;
                    if (!tileSet.TryGetTileProperties(tileIndex, out propertyList))
                    {
                        continue;
                    }
                    string tileType = propertyList["type"];
                    var tile = tileFactory.GetTile(tileType, new Vector2(x * map.TileWidth, y * map.TileHeight));
                    allTiles.Add(tile);
                }
            }
        }

        protected override void UnloadContent() { }

        protected override void Update(GameTime gameTime)
        {            
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            var windowArea = new Rectangle(0, 0, this.Window.ClientBounds.Height, this.Window.ClientBounds.Height);
            map.Draw(spriteBatch, windowArea, new Vector2(0, 0));

            foreach (var tile in this.allTiles)
            {
                tile.Draw();
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }       
    }
}
